package org.sgs.toolbox.sql;

import java.sql.Date;
import java.util.Map;

public class GenericRecord {
	
	private Object[] row;
	Map<String, Integer> nameToIndexMap;

	public GenericRecord(Object[] row, Map<String, Integer> nameToIndexMap){
		this.row = row;
		this.nameToIndexMap = nameToIndexMap;
	}

	public Boolean getBoolean(int index){
		return (Boolean) row[index];
	}

	public byte getByte(int index){
		return (Byte) row[index];
	}

	public Date getDate(int index){
		return (Date) row[index];
	}

	public Double getDouble(int index){
		return (Double) row[index];
	}

	public Float getFloat(int index){
		return (Float) row[index];
	}

	public Integer getInt(int index){
		return (Integer) row[index];
	}

	public Long getLong(int index){
		return (Long) row[index];
	}

	public Short getShort(int index){
		return (Short) row[index];
	}

	public String getString(int index){
		return (String) row[index];
	}
	
	public Object getObject(int index){
		return row[index];
	}
	
	public Boolean getBoolean(String columnName){
		int index = nameToIndexMap.get(columnName);
		return (Boolean) row[index];
	}

	public byte getByte(String columnName){
		int index = nameToIndexMap.get(columnName);
		return (Byte) row[index];
	}

	public Date getDate(String columnName){
		int index = nameToIndexMap.get(columnName);
		return (Date) row[index];
	}

	public Double getDouble(String columnName){
		int index = nameToIndexMap.get(columnName);
		return (Double) row[index];
	}

	public Float getFloat(String columnName){
		int index = nameToIndexMap.get(columnName);
		return (Float) row[index];
	}

	public Integer getInt(String columnName){
		int index = nameToIndexMap.get(columnName);
		return (Integer) row[index];
	}

	public Long getLong(String columnName){
		int index = nameToIndexMap.get(columnName);
		return (Long) row[index];
	}

	public Short getShort(String columnName){
		int index = nameToIndexMap.get(columnName);
		return (Short) row[index];
	}

	public String getString(String columnName){
		int index = nameToIndexMap.get(columnName);
		return (String) row[index];
	}
	
	public Object getObject(String columnName){
		int index = nameToIndexMap.get(columnName);
		return row[index];
	}
	
	public boolean columnNameExists(String columnName){
		return nameToIndexMap.get(columnName) != null;
	}
	
	public int getColumnCount(){
		return row.length;
	}
	
	@Override
	public String toString(){
		return toString(" ");
	}
	
	public String toString(String delimiter){
		StringBuilder sb = new StringBuilder();
		for(int i = 0; i < getColumnCount(); i++){
			sb.append(row[i]);
			if(i < getColumnCount() - 1){
				sb.append(delimiter);
			}
		}
		return sb.toString();
	}
	
}
